import { IsString, IsNumberString ,IsNotEmpty,ValidationOptions, IsNumber, IsBoolean, isNumber, Length} from 'class-validator';
import { ApiProperty} from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';


export class PageAndSize {
	@ApiProperty({
		description:"页码",
		required:false
	})
	@Transform(({ value }) => toNumber(value, { default: 1, min: 1 }))
	@IsNumber()
	page: number=1;

	@ApiProperty({
		description:"每页数量",
		required:false
	})
	@Transform(({ value }) => toNumber(value, { default: 20, min: 1 }))
	@IsNumber()
	size: number=20;
}
interface ToNumberOptions {
	default?: number;
	min?: number;
	max?: number;
}
function toNumber(value: string, opts: ToNumberOptions = {}): number {
	let newValue: number = Number.parseInt(value || String(opts.default), 10);

	if (Number.isNaN(newValue)) {
	  newValue = opts.default;
	}

	if (opts.min) {
	  if (newValue < opts.min) {
		newValue = opts.min;
	  }

	  if (newValue > opts.max) {
		newValue = opts.max;
	  }
	}

	return newValue;
}

export class DocBase {
	@ApiProperty({
		description:"id",
	})
	_id: string;

	@ApiProperty({
		description:"状态",
	})
	status:string;

	@ApiProperty({
		description:"创建时间",
	})
	createdAt:string;
	@ApiProperty({
		description:"更新时间",
	})
	updatedAt:string;
}

export class TypesQuery {
	@ApiProperty({
		description:"接口路径",
	})
	path:string;
}

/**mongodb通用id实体 */
export class MongodbId {
	@ApiProperty({
		description:"记录id"
	})
	@IsString()
	@Length(24,24)
	id: string;
}

export class NullableMongodbId {
	@ApiProperty({
		description:"记录id"
	})
	id: string;
}
export interface PropsValue {
    value: number;
    type:  string;
}
